package com.taptech.doufu.ui.view.choice;

import android.content.Context;
import android.graphics.Typeface;
import androidx.annotation.Nullable;

import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.bean.DFHomeCategoryBean;
import com.taptech.doufu.bean.EventBean;
import com.taptech.doufu.bean.choice.BlockModel;
import com.taptech.doufu.bean.choice.ChoiceTopicBean;
import com.taptech.doufu.constant.UmengEventName;
import com.taptech.doufu.listener.NormalInterface;
import com.taptech.doufu.ui.view.readview.util.ScreenUtils;
import com.taptech.doufu.util.ScreenUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ChoiceTopicRankComplexView extends LinearLayout {
    private Context context;
    private LinearLayout layRight;
    private FrameLayout layContent;
    private int index = 0;
    private Map<Integer, ChoiceTopicRankPagerView> cache = new HashMap<>();
    private EventBean eventData;
    private int tabIndex = 0;

    public ChoiceTopicRankComplexView(Context context) {
        super(context);
    }

    public ChoiceTopicRankComplexView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ChoiceTopicRankComplexView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public ChoiceTopicRankComplexView(Context context, ChoiceTopicBean bean) {
        this(context);
        View view = LayoutInflater.from(context).inflate(R.layout.topic_choice_rank_complex_view, null);
        layRight = view.findViewById(R.id.layRight);
        layContent = view.findViewById(R.id.svContent);
        eventData = bean.getEventData();
        this.context = context;
        setLayoutRight(bean.getBlocks(),bean);
        updateUI(bean);
        addView(view);
    }


    private void updateUI(ChoiceTopicBean bean) {
        layContent.removeAllViews();
        if (cache.containsKey(index)) {
            ChoiceTopicRankPagerView content = cache.get(index);
            content.switchViewPager(tabIndex);
            layContent.addView(content);
        }else{
            final List<DFHomeCategoryBean.Item> moreData = new ArrayList<>();
            for (int i = 0; i < bean.getBlocks().size(); i++) {
                DFHomeCategoryBean.Item model = new DFHomeCategoryBean.Item();
                model.setTitle(bean.getBlocks().get(i).getTitle());
                model.setUrl(bean.getBlocks().get(i).getMoreUrl());
                moreData.add(model);
            }
            ChoiceTopicRankPagerView content = new ChoiceTopicRankPagerView(context, bean.getBlocks().get(index).getList(),
                    bean.getNovelList(), moreData, index, new NormalInterface.OnIntegerListener() {
                @Override
                public void onResult(int index) {
                    tabIndex = index;
                }
            },eventData);
            content.switchViewPager(tabIndex);
            cache.put(index, content);
            layContent.addView(content);
        }
    }

    private void setTextViewColor(TextView textView, boolean isSelect) {
        if (WeMediaApplication.getInstance().isDayNightMode_Night) {
            if (isSelect) {
                textView.setBackgroundResource(R.drawable.cycle_red_night);
                textView.setTextColor(getResources().getColor(R.color.white));

            }else{
                textView.setBackgroundResource(R.drawable.bg_tag_gray_dark);
                textView.setTextColor(getResources().getColor(R.color.text_dark));

            }
        }else{
            if (isSelect) {
                textView.setTextColor(getResources().getColor(R.color.white));
                textView.setBackgroundResource(R.drawable.cycle_red);
            }else{
                textView.setBackgroundResource(R.drawable.bg_tag_gray);
                textView.setTextColor(getResources().getColor(R.color.text_color_6C788A));
            }
        }
        textView.setPadding(ScreenUtils.dpToPxInt(20f), ScreenUtils.dpToPxInt(5f), ScreenUtils.dpToPxInt(20f), ScreenUtils.dpToPxInt(5f));

    }

    private void setLayoutRight(List<BlockModel> list,final ChoiceTopicBean bean) {
        if (list == null) {
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            BlockModel blockModel = list.get(i);
            TextView text = new TextView(context);
            Typeface font = Typeface.create("sans-serif-medium", Typeface.NORMAL);
            text.setTypeface(font);
            text.setClickable(true);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, ScreenUtils.dpToPxInt(27f));
            params.rightMargin = ScreenUtil.dip2px(context,10);
            text.setLayoutParams(params);
            text.setTextSize(13f);
            text.setText(blockModel.getTitle());
            text.setGravity(Gravity.CENTER);
            setTextViewColor(text,index == i);
            final int tempIndex = i;
            text.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    ChoiceUtils.addEvent(context,UmengEventName.JINGXUAN_CARD_TR_TAB,eventData);
                    index = tempIndex;
                    for (int j = 0; j < layRight.getChildCount(); j++) {
                        TextView tv = (TextView) layRight.getChildAt(j);
                        setTextViewColor(tv,index == j);
                    }
                    updateUI(bean);
                }
            });
            layRight.addView(text);
        }
    }

}
